package com.caigou.modules.login.ui

import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.container.BaseFragment
import com.caigou.modules.login.R
import com.caigou.modules.login.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/09/18
 */
@Route(path = RouterPath.Login.PAGE_LOGIN)
@AndroidEntryPoint
class LoginActivity : BaseActivity() {

    private lateinit var binding: ActivityLoginBinding
    private var launcherFragment: BaseFragment? = null
    private var registerFragment: BaseFragment? = null
    private var verifyFragment: BaseFragment? = null
    private var forgetFragment: BaseFragment? = null

    var smsCode = ""
    var mobile = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.loginFlContainer.id
        initView()
    }


    private fun initView() {
        switchFragment(RouterPath.Login.TAG_LAUNCHER)
    }

    override fun switchFragment(tag: String) {
        when (tag) {
            RouterPath.Login.TAG_LAUNCHER -> {
                if (launcherFragment == null) {
                    launcherFragment = LauncherFragment()
                }
                switchFragment(launcherFragment!!, tag)
            }
            RouterPath.Login.TAG_VERIFY -> {
                if (verifyFragment == null) {
                    verifyFragment = VerifyFragment()
                }
                switchFragment(verifyFragment!!, tag)
            }
            RouterPath.Login.TAG_REGISTER -> {
                if (registerFragment == null) {
                    registerFragment = RegisterFragment()
                }
                switchFragment(registerFragment!!, tag)
            }
            RouterPath.Login.TAG_FORGET -> {
                if (forgetFragment == null) {
                    forgetFragment = ForgetFragment()
                }
                switchFragment(forgetFragment!!, tag)
            }
        }
    }

    override fun switchFragment(fragment: BaseFragment, target: String) {
        if (fragmentStack.contains(target)) {
            // 栈里最后一个与tag相同才执行删除
            if (fragmentStack.size > 1 && !fragmentStack.last.equals(target)) {
                fragmentStack.removeLast()
            }
        } else {
            fragmentStack.add(target)
        }

        val transaction = supportFragmentManager.beginTransaction()
        transaction.setCustomAnimations(R.anim.comm_popup, R.anim.comm_push)
        var targetFragment = supportFragmentManager.findFragmentByTag(currentFragment)
        if (targetFragment != null && target != currentFragment) {
            targetFragment.onPause()
            transaction.hide(targetFragment)
        }
        targetFragment = supportFragmentManager.findFragmentByTag(target)
        if (targetFragment == null) {
            transaction.add(R.id.login_fl_container, fragment, target)
        } else {
            targetFragment.onResume()
            transaction.show(targetFragment)
        }
        currentFragment = target
        transaction.commitAllowingStateLoss()
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            if (it.action == MotionEvent.ACTION_DOWN) {
                val v = currentFocus
                if (isShouldHideInput(v, it)) {
                    val imm =
                        this@LoginActivity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(v!!.windowToken, 0)
                }
                return super.dispatchTouchEvent(it)
            }
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else onTouchEvent(ev)
    }

    override fun onBackPressed() {
        if (fragmentStack.isNotEmpty() && fragmentStack.size > 1) {
            when (fragmentStack.last) {
                RouterPath.Login.TAG_REGISTER -> registerFragment?.onBack()
                RouterPath.Login.TAG_VERIFY -> verifyFragment?.onBack()
                RouterPath.Login.TAG_FORGET -> forgetFragment?.onBack()
            }
            fragmentStack.removeLast()
            switchFragment(fragmentStack.last)
        } else {
            super.onBackPressed()
        }
    }
}